"use strict";
// string number boolean null undefined symbol bigint object
let str = "hello";
// any 类型
let a = "aaa";
let aa = null;
// 字面量类型
const b = "hello";
const c = null;
let d;
d = "world";
// d = "aaa"; // 报错
// 联合类型
let v1 = undefined;
v1 = "aaa";
v1 = 123;
v1 = true;
// v1 = null; // 报错
let v2;
v2 = "男";
let v3 = "UP";
v3 = "LEFT";
v3 = "RIGHT";
console.log(v3);
// 数组类型
// 数组类型可以有两种表示方式
// 类型[]
// Array<类型>
const arr1 = ["a", "b", "c"];
const arr2 = [1, 2, 3, 4, 5];
const arr3 = [1, 2, 3, 4, 5];
const arr4 = ["a", "b", "c"];
// 空数组赋值，在strict严格检查下，默认是any类型
// 但是这个和其他类型检查配置相关noImplicitAny
const arr5 = [];
arr5.push(1);
arr5.push("abc");
// 数组也能用联合类型
const arr6 = ["a", 1, "b", 2];
const arr7 = ["a", 1, "b", 2];
// 注意和下面的写法进行区分
const arr8 = [1, 2, 3, 4];
const arr9 = ["a", "b", "c"];
// 元组类型
const tuple1 = ["a", 1];
const tuple2 = [1, 2, 3];
// 使用场景：坐标
// let position:number[] = [39.5436, 117.2312];
let position = [39.5436, 117.2312];
console.log(position);
// 一个容易混淆的赋值情况
let value = [];
let tuple3 = []; // 空元祖
function add(a, b) {
    return a + b;
}
const r = add("a", "b");
// 可选参数
// 可以在某些参数后面加上问号?,表示参数可以不用一定要传递
// 注意：可选参数必须要在必选参数后面
function sum1(a, b, c) {
    console.log(a, b, c);
}
sum1(1, 2);
// 默认参数本身就是可选参数
function sum2(a, b, c = 10) {
    console.log(a, b, c);
}
sum2(1, 2);
// 剩余参数
const fn = (a, b, ...rest) => {
    console.log("----", a, b, rest);
};
fn(1, 2, 3, 4, 5, 6);
// 如果函数没有返回值类型，默认返回类型是void
// 泛型
function identity(arg) {
    return arg;
}
let output1 = identity("myString");
// 也可以使用类型推断
let output2 = identity("myString");
let output3 = identity(123);
console.log(output1, output2, output3);
function getTuple1(a, b) {
    return [a, b];
}
const as1 = getTuple1("hello", "world");
console.log(as1);
function getTuple2(a, b) {
    return [a, b];
}
const as2 = getTuple2("hello", 123);
console.log(as2);
const as3 = getTuple2("hello", 123);
console.log(as3);
function myNumbersFilter(arr, callback) {
    const result = [];
    for (let i = 0; i < arr.length; i++) {
        const item = arr[i];
        if (callback(item, i)) {
            result.push(item);
        }
    }
    return result;
}
const filterArr = myNumbersFilter([1, 2, 3, 4, 5], (item) => {
    return item % 2 === 0;
});
console.log(filterArr);
// 使用泛型参数定义filter函数
function myFilter(arr, callback) {
    const result = [];
    for (let i = 0; i < arr.length; i++) {
        const item = arr[i];
        if (callback(item, i)) {
            result.push(item);
        }
    }
    return result;
}
const filterArr2 = myFilter(["xxx.js", "aaa.java", "bbb.ts", "ccc.ts"], item => item.endsWith(".ts"));
console.log(filterArr2);
// 对象字面量类型
const v4 = "hello";
const obj1 = {
    id: 1,
    name: "jack",
    age: 20
};
const obj2 = {
    id: 1,
    name: "jack",
    age: 20,
    sex: "男"
};
const arr10 = [
    {
        name: "jack",
        age: 20
    },
    {
        name: "tom",
        age: 21
    }
];
function getInfo(user) {
    // todos...
    return [
        {
            name: "jack",
            age: 20
        },
        {
            name: "tom",
            age: 21
        }
    ];
}
// let user = { name: "lily", age: 21, sex: "女" };
getInfo({ name: "lily", age: 21 });
const obj3 = {
    id: 1,
    name: "jack",
    age: 20
};
const obj4 = {
    id: 1,
    name: "jack",
    age: 20
};
// 在函数和数组中使用就非常方便了
function fn1(user) {
    console.log(user.name);
}
const persons = [
    {
        id: 1,
        name: "rose",
        age: 20
    },
    {
        id: 2,
        name: "jack",
        age: 19
    }
];
const book = {
    id: 1,
    name: "javascript",
    price: 100,
    show(id) {
        console.log(id);
    },
    filter(id) {
        console.log(id);
    },
    info(id, name) {
        return "hello";
    },
    author: {
        id: 1,
        name: "jack",
        age: 20
    }
};
